package csel.view;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JToggleButton;
import javax.swing.JPanel;
import javax.swing.ImageIcon;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.LinkedList;
import java.util.List;

import csel.model.Item;
import csel.model.Entity;
import csel.model.Inventory;
import csel.model.EquippableItem;
import csel.model.Observer;
import csel.model.TakeableItem;
import csel.model.UseableItem;


/*
 * Reasons for Existence: 
 * Team Members Responsible
 * Implementation: Alex, Andrew
 * Testing: Alex, Andrew
 */
public class InventoryViewer extends JPanel implements ActionListener, Observer
{
	ButtonGroup itemButtons;
	LinkedList<JToggleButton> equippableButtons;
	LinkedList<JToggleButton> useableButtons;
	JButton use;
	JButton drop;
	JButton equip;
	JPanel buttonPane;
	JPanel itemsPane;
	
	public InventoryViewer(Entity e, ActionListener a)
	{
		e.addObserver(this);
		
		buttonPane = new JPanel();
		itemsPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane,BoxLayout.X_AXIS));
		
		use = new JButton("Use");
		equip = new JButton("Equip");
		drop = new JButton("Drop");
		use.setActionCommand("stuse");
		equip.setActionCommand("stequip");
		drop.setActionCommand("stdrop");
		use.addActionListener(a);
		equip.addActionListener(a);
		drop.addActionListener(a);
		
		modelUpdate(e);
	}

	public TakeableItem[] getSelectedItems()
	{
		LinkedList<TakeableItem> selectedItems = new LinkedList<TakeableItem>();
		int size = equippableButtons.size();
		if (size == 0)
		{
			return null;
		}
		for (int a = 0; a < size; ++a)
		{
			if (equippableButtons.get(a).getSelectedObjects() != null)
				selectedItems.add((TakeableItem) equippableButtons.get(a).getClientProperty("myItem"));
		}
		size = useableButtons.size();
		for (int a = 0; a < size; ++a)
		{
			if (useableButtons.get(a).getSelectedObjects() != null)
				selectedItems.add((TakeableItem) useableButtons.get(a).getClientProperty("myItem"));
		}
		Object[] items = selectedItems.toArray();
		int itemsLength = items.length;
		TakeableItem[] takeables = new TakeableItem[itemsLength];
		for (int a = 0; a < itemsLength; ++a)
		{
			takeables[a] = (TakeableItem) items[a];
		}
		return takeables;
	}
	
	public void modelUpdate(Object o)
	{
		Entity e = ((Entity) o);
		Inventory inv = e.getInventory();
		removeAll();
		List<EquippableItem> eList = inv.getEquippableItems();
		List<UseableItem> uList = inv.getUseableItems();
        
		buttonPane.add(use);
		buttonPane.add(drop);
		buttonPane.add(equip);
		
		this.setLayout(new BoxLayout(this,BoxLayout.Y_AXIS));
		
		itemButtons = new ButtonGroup();
		useableButtons = new LinkedList<JToggleButton>();
		equippableButtons = new LinkedList<JToggleButton>();
		int size = eList.size();
		int totalSize = size + uList.size();
		
		itemsPane = new JPanel();
		itemsPane.setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();
		int rowNum = 0;
		
		for (int b = 0; b < size; ++b)
		{
			JToggleButton j = new JToggleButton(new ImageIcon(RepresentationFactory.getInstance().get(eList.get(b)).getSimpleImage()));
			j.setActionCommand("equippable");
			j.addActionListener(this);
			j.putClientProperty("myItem", (TakeableItem) eList.get(b));
			j.setPreferredSize(new Dimension(50,50));
			equippableButtons.add(j);
			itemButtons.add(j);
			c.gridx = 0;
			c.gridy = rowNum;
			itemsPane.add(j,c);
			c.gridx = 1;
			c.gridy = rowNum++;
			itemsPane.add(new JLabel(eList.get(b).getDescription()),c);
		}
		size = uList.size();
		for (int b = 0; b < size; ++b)
		{
			JToggleButton j = new JToggleButton(new ImageIcon(RepresentationFactory.getInstance().get(uList.get(b)).getSimpleImage()));
			j.setActionCommand("useable");
			j.addActionListener(this);
			j.putClientProperty("myItem", uList.get(b));
			j.setMaximumSize(new Dimension(50,50));
			useableButtons.add(j);
			itemButtons.add(j);
			c.gridx = 0;
			c.gridy = rowNum;
			itemsPane.add(j,c);
			c.gridx = 1;
			c.gridy = rowNum++;
			itemsPane.add(new JLabel(uList.get(b).getDescription()),c);
		}
		buttonPane.setOpaque(false);
		itemsPane.setOpaque(false);
		add(buttonPane);
		add(itemsPane);

		revalidate();
        //repaint();
	}
	
	public void setEntity(Entity e)
	{
		e.addObserver(this);
		modelUpdate(e);
	}
	
	public void actionPerformed(ActionEvent e)
	{
		if (e.getActionCommand().equals("equippable"))
		{
			use.setEnabled(false);
			equip.setEnabled(true);
		}
		else
		{
			use.setEnabled(true);
			equip.setEnabled(false);
		}
	}

}